package com.app.twth.ui.tally.lpn;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.recyclerview.widget.RecyclerView;

import com.app.twth.R;
import com.app.twth.databinding.ItemTallyLpnSizeListBinding;
import com.app.twth.interfaces.MyOnClickListener;

import java.util.ArrayList;
import java.util.List;


public class TallyLPNSizeAdapter extends RecyclerView.Adapter<TallyLPNSizeAdapter.MyViewHolder> {
    private LayoutInflater mInflater;
    private Context mContext;
    private List<TalliedSize> data = new ArrayList<>();
    private MyOnClickListener locationListener;

    public TallyLPNSizeAdapter(Context mContext, List<TalliedSize> data, MyOnClickListener locationListener) {
        this.mContext = mContext;
        this.data = data;
        this.mInflater = LayoutInflater.from(mContext);
        this.locationListener = locationListener;
    }

    public void setData(List<TalliedSize> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(ItemTallyLpnSizeListBinding.inflate(mInflater, parent, false));
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.setData(position);
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        ItemTallyLpnSizeListBinding itemBinding;

        public MyViewHolder(ItemTallyLpnSizeListBinding itemBinding) {
            super(itemBinding.getRoot());
            this.itemBinding = itemBinding;
        }

        public void setData(int position) {
            itemView.setTag(position);
            TalliedSize tallyLPN = data.get(position);
            if (tallyLPN.isSelect()){
                itemBinding.ivSize.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ic_baseline_radio_button_checked_24));
            }else {
                itemBinding.ivSize.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ic_baseline_radio_button_unchecked_24));
            }
            itemView.setOnClickListener(v->{
                if (locationListener!=null){
                    locationListener.OnClick(position);
                }
            });
            itemBinding.tvInventory.setText(tallyLPN.getPalletLength() + "*"+tallyLPN.getPalletWidth());
        }
    }
}
